package com.jlcloud.core.oauth2.granter;

import com.jlcloud.core.oauth2.handler.PasswordHandler;
import com.jlcloud.core.oauth2.provider.OAuth2Request;
import com.jlcloud.core.oauth2.service.OAuth2ClientService;
import com.jlcloud.core.oauth2.service.OAuth2User;
import com.jlcloud.core.oauth2.service.OAuth2UserService;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * PasswordTokenGranter
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@Component
public class PasswordTokenGranter extends AbstractTokenGranter {

	public PasswordTokenGranter(OAuth2ClientService clientService, OAuth2UserService userService, PasswordHandler passwordHandler) {
		super(clientService, userService, passwordHandler);
	}

	@Override
	public String type() {
		return PASSWORD;
	}

	@Override
	public OAuth2User user(OAuth2Request request) {
		OAuth2User user = super.user(request);
		return Optional.ofNullable(this.enhancer)
			.map(enhancer -> enhancer.enhance(user, request))
			.orElse(user);
	}

}
